package com.ningxia.water.controller.geographic;

import com.ningxia.water.entity.TsApplicantInfo;
import com.ningxia.water.entity.ZtTownInfo;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.IZtTownInfoService;
import com.ningxia.water.utils.CommonUtils;
import com.ningxia.water.vo.out.ApplicantVo;
import com.ningxia.water.vo.out.ZonePidVo;
import com.ningxia.water.vo.out.ZoneVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangzhen
 * @create 2021-05-14 21:03
 */
@RestController
@RequestMapping("/town")
@Api(tags = {"地理信息-3-乡镇管理API"})
public class TownController {

    @Autowired
    private IZtTownInfoService townInfoService;

    @GetMapping("/gettowninfo")
    @ApiOperation("获取全部乡镇地理信息")
    public Result getTownInfo() {
        List<ZoneVo> list = townInfoService.getTownInfos();
        return ResultResponse.getSuccessResult(list);
    }

    @PostMapping("/inserttowninfo")
    @ApiOperation(value = "新增乡镇")
    public Result insertTownInfo(@RequestBody ZtTownInfo townInfo) {
        return CommonUtils.commonResult(townInfoService.save(townInfo));
    }

    @PutMapping("/updatetowninfo")
    @ApiOperation(value = "根据ID更新乡镇")
    public Result updateTownInfo(@RequestBody ZtTownInfo townInfo) {
        return CommonUtils.commonResult(townInfoService.updateById(townInfo));
    }


    @DeleteMapping("/deletetowninfo")
    @ApiOperation(value = "根据id删除乡镇")
    public Result deleteTownInfo(@RequestParam("townId") Integer townId) {
        Integer count = townInfoService.findByTownId(townId);
        if (count == 0) {
            return CommonUtils.commonResult(townInfoService.removeById(townId));
        }
        return ResultResponse.getFailResult("该乡镇下含有村庄");

    }

    @GetMapping("/getallapplicants")
    @ApiOperation(value = "获取该乡镇下的所有用水户")
    public Result getAllApplicants(@RequestParam("townId") Integer townId){
        List<ApplicantVo> list = townInfoService.getAllApplicants(townId);
        return ResultResponse.getSuccessResult(list);
    }

    @GetMapping("/gettownrelationship")
    @ApiOperation(value = "获取乡镇及其对应的区县")
    public Result getTownRelationship() {
        List<ZonePidVo> list = townInfoService.getTownRelationship();
        return ResultResponse.getSuccessResult(list);
    }
}
